<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
use think\facade\Session;
use think\facade\Config;
use think\Db;
use TCPDF;

class Cross extends Base
{
	

	public function xunhaun(Request $request){
		$route_name=request()->param()['brand'];
		$data['base_data']=$this->base_data;
        $data['crumbs']=get_crumbs(1655);/*面包屑*/ 
        $data['seo_title']="Bearing Cross Reference ".$route_name." | Lily Bearing";
        $data['seo_keywords']="Bearing Cross Reference ".$route_name." | Lily Bearing";
        $data['seo_desc']="Bearing Cross Reference ".$route_name." | Lily Bearing";
        $cb_id=2;
        $get_data=$this->get_data($cb_id);

		$data['lily_cross']=$get_data['lily_cross'];
		$data['title_data']=$get_data['title_data'];
		//dump($lily_cross);exit;
		$this->assign($data);
		return $this->fetch();
	}

	public function get_data($cb_id){
		$lily_cross=Db::table('lily_cross')->field('cb_id,part_number,cross_number')->where('cb_id',$cb_id)->select();

		$sql="SELECT cb_id FROM lily_cross WHERE part_number IN (select part_number from lily_cross where cb_id=".$cb_id." group by part_number) and cb_id!=".$cb_id."  GROUP BY cb_id";
		$cb_id_arr=Db::query($sql);
		$title_data=array();
		foreach ($cb_id_arr as $k => $v) {
			$title_data[]=Db::table('lily_cross_brand')->field('name')->where('id','=',$v['cb_id'])->find();
		}
		
		

		foreach ($lily_cross as $key => $value) {
			$url_product=Db::table('lily_products')->field('url_product')->where('part_number',$value['part_number'])->find();
			$lily_cross[$key]['url_product']=$url_product['url_product'];

			$other_brand=array();

			foreach ($cb_id_arr as $k => $v) {
				$lily_cross_other=Db::table('lily_cross')->field('cross_number')->where([['cb_id','=',$v['cb_id']],['part_number','=',$value['part_number']]])->find();
				if(empty($lily_cross_other)){
                    $other_brand[$k]['part_number']='';
				}else{
                    $other_brand[$k]['part_number']=$lily_cross_other['cross_number'];
				}
			}
			$lily_cross[$key]['other_brand']=$other_brand;
		}


		$data['lily_cross']=$lily_cross;
		$data['title_data']=$title_data;
		return $data;
	}

	public function index(){
		$lily_cross=Db::table('lily_cross')->field('pid,id')->select();
		foreach ($lily_cross as $key => $value) {
			$pidr=explode(',', $value['pid']);
			$part_number=Db::table('lily_products')->field('part_number')->where('id',$pidr[0])->find();
			echo "update lily_cross set part_number='".$part_number['part_number']."' where id=".$value['id'].";</br>";
		}
	}

	public function pdf(Request $request){
        $cb_id=request()->get()['cb_id'];
        $get_data=$this->get_data($cb_id);
        $str='<table border="1" cellpadding="1" cellspacing="1"><tr><th>LILY</th><th>NMB</th>';
        if(!empty($get_data['title_data'])){
        	foreach ($get_data['title_data'] as $key => $value) {
        		$str.="<th>".$value['name']."</th>";
        	}
        }
        $str.="</tr>";
        foreach ($get_data['lily_cross'] as $key => $value) {
        	$str.='<tr><td><a href="/products/'.$value['url_product'].'/">'.$value['part_number'].'</a></td><td>'.$value['cross_number'].'</td>';
        	if(!empty($value['other_brand'])){
        		foreach ($value['other_brand'] as $k1=>$v1) {
                    if(!empty($v1['part_number'])){
                        $str.="<td>".$v1['part_number']."</td>";
                    }else{
                        $str.='<td>-</td>';
                    }
                }    
        	}
        	$str.="</tr>";
        }
        $str.="</table>";

        $custom_layout = array(297,297);
        $pdf = new TCPDF('Portrait', PDF_UNIT, $custom_layout, true, 'UTF-8', TRUE);
        
        $pdf->SetCreator('-');
        $pdf->SetTitle('NMB Cross Reference Charts');
        $pdf->SetAuthor('LILY BEARING');
        $pdf->SetSubject('-');
        $pdf->SetKeywords('-');
        

        // remove default header/footer
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(2, 0, 2,true);
        //$pdf->SetFooterMargin(0);

        // set auto page breaks
        $pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once(dirname(__FILE__).'/lang/eng.php');
            $pdf->setLanguageArray($l);
        }

        // ---------------------------------------------------------

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $bMargin = $pdf->getBreakMargin();
        // get current auto-page-break mode
        //$auto_page_break = $pdf->getAutoPageBreak();
        // disable auto-page-break
        $pdf->SetAutoPageBreak(true, 0);
        // set bacground image
        
        //$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $pdf->setPageMark();

        $pdf->SetFillColor(255, 255, 255);

        $pdf->writeHTML($str, true, false, false, false, '');
        ob_end_clean();
        $pdf->Output("A.pdf", 'I');
	}

}